import type { TreeNodeData } from './type'
import { getTemplateTree } from '@/api/modules/template'

// eslint-disable-next-line
async function getData (code: string) {
  return new Promise<any>((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        data: {
          modelName: '电力计量图',
          modelNodes: [
            {
              id: 2,
              parentId: 1,
              level: 1,
              children: [
                {
                  id: 3,
                  parentId: 2,
                  level: 2,
                  children: [
                    {
                      id: 5,
                      parentId: 3,
                      level: 3,
                      children: null,
                      name: '办公室12',
                      code: null,
                      trees: '1,2,3,5,',
                    },
                    {
                      id: 6,
                      parentId: 3,
                      level: 3,
                      children: null,
                      name: '厂房',
                      code: null,
                      trees: '1,2,3,6,',
                    },
                  ],
                  name: '一分厂',
                  code: null,
                  trees: '1,2,3,',
                },
                {
                  id: 4,
                  parentId: 2,
                  level: 2,
                  children: [
                    {
                      id: 7,
                      parentId: 4,
                      level: 3,
                      children: null,
                      name: '办公室',
                      code: null,
                      trees: '1,2,4,7,',
                    },
                    {
                      id: 8,
                      parentId: 4,
                      level: 3,
                      children: null,
                      name: '厂房',
                      code: null,
                      trees: '1,2,4,8,',
                    },
                  ],
                  name: '二分厂',
                  code: null,
                  trees: '1,2,4,',
                },
              ],
              name: '全厂用电',
              code: null,
              trees: '1,2,',
            },
          ],
        },
      })
    }, 400)
  })
}
export default function useApplicationModelTree() {
  const [loading, setLoading] = useState(false)
  const [data, setData] = useShallowState<TreeNodeData[]>([])
  const [modelName, setModelName] = useState('')

  async function getTreeData(code: string) {
    setLoading(true)
    try {
      const { data } = await getTemplateTree(code)
      setModelName(data.name)
      setData(data.children ?? [])
    }
    finally {
      setLoading(false)
    }
  }

  return {
    modelName,
    loading,
    data,
    getTreeData,
  }
}
